﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WCS.Infrastructure.Dto.Response.Epaylinks;

namespace WCS.Infrastructure.Dto.Request.Epaylinks
{
    /// <summary>
    /// 账户分账请求对象
    /// </summary>
    public class AccountSplitRequest
    {
        /// <summary>
        /// 版本号，默认1.0
        /// </summary>
        public string Version { get; set; }

        /// <summary>
        /// 商户编号
        /// </summary>
        public string CustomerCode { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string OutTradeNo { get; set; }

        /// <summary>
        /// 总商品金额，单位分
        /// </summary>
        public long CommodityAmount { get; set; }

        /// <summary>
        /// 商品信息列表
        /// </summary>
        public List<CommodityInfo> CommodityInfoList { get; set; }

        /// <summary>
        /// 订单分账信息列表
        /// </summary>
        public List<SplitInfo> SplitInfoList { get; set; }

        /// <summary>
        /// 分账结果通知地址
        /// </summary>
        public string NotifyUrl { get; set; }

        /// <summary>
        /// 商户备注
        /// </summary>
        public string AttachData { get; set; }

        /// <summary>
        /// 随机字符串
        /// </summary>
        public string NonceStr { get; set; }

        ///// <summary>
        ///// 验证请求参数有效性
        ///// </summary>
        //public bool IsValid()
        //{
        //    // 验证商品金额总和
        //    if (CommodityInfoList?.Sum(c => c.CommodityOrderAmount) != CommodityAmount)
        //    {
        //        return false;
        //    }

        //    // 验证手续费扣除对象
        //    if (IsProcedureFeeRequired())
        //    {
        //        // 检查是否有且仅有一个收单手续费扣除对象(1或3)
        //        var acquirerFeeCount = SplitInfoList?.Count(s => s.IsProcedureCustomer == 1 || s.IsProcedureCustomer == 3) ?? 0;
        //        if (acquirerFeeCount != 1)
        //        {
        //            return false;
        //        }

        //        // 检查是否有且仅有一个分账手续费扣除对象(2或3)
        //        var splitFeeCount = SplitInfoList?.Count(s => s.IsProcedureCustomer == 2 || s.IsProcedureCustomer == 3) ?? 0;
        //        if (splitFeeCount != 1)
        //        {
        //            return false;
        //        }
        //    }

        //    return true;
        //}

        ///// <summary>
        ///// 判断是否需要指定手续费扣除对象
        ///// </summary>
        //private bool IsProcedureFeeRequired()
        //{
        //    // 这里需要根据业务规则判断，示例中假设为true
        //    return true;
        //}
    }

    /// <summary>
    /// 商品信息
    /// </summary>
    public class CommodityInfo
    {
        /// <summary>
        /// 商品订单号
        /// </summary>
        public string CommodityOrderNo { get; set; }

        /// <summary>
        /// 订单类型：0-线上订单(默认) 1-POS订单
        /// </summary>
        public string CommodityOrderType { get; set; } = "0";

        /// <summary>
        /// 商品订单金额，单位分
        /// </summary>
        public long CommodityOrderAmount { get; set; }

        /// <summary>
        /// 商品订单时间，格式YYYYMMDDHHMMSS
        /// </summary>
        public string CommodityOrderTime { get; set; }

        /// <summary>
        /// 商品描述
        /// </summary>
        public string CommodityDescription { get; set; }
    }

    /// <summary>
    /// 订单分账信息
    /// </summary>
    public class SplitInfo
    {
        /// <summary>
        /// 分账目标商户编号
        /// </summary>
        public string CustomerCode { get; set; }

        /// <summary>
        /// 分账金额，单位分
        /// </summary>
        public long Amount { get; set; }

        /// <summary>
        /// 分账对象属性：
        /// 0-常规分账对象
        /// 1-收单手续费扣除对象
        /// 2-分账手续费扣除对象
        /// 3-收单和分账手续费扣除对象
        /// </summary>
        public int? IsProcedureCustomer { get; set; }

        /// <summary>
        /// 结算周期，拆单分账时有效，默认30
        /// </summary>
        public int? SettleCycle { get; set; } = 30;
    }


    /// <summary>
    /// 分账结果异步通知请求DTO
    /// </summary>
    public class SplitResultNotificationRequest:EpaylinksResponseDto
    {
        /// <summary>
        /// 商户编号
        /// </summary>
        public string CustomerCode { get; set; }

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string OutTradeNo { get; set; }

        /// <summary>
        /// 易票联订单号
        /// </summary>
        public string TransactionNo { get; set; }

        /// <summary>
        /// 易票联分账单号
        /// </summary>
        public string? SplitTransactionNo { get; set; }

        /// <summary>
        /// 商户分账单号
        /// </summary>
        public string? OutSplitTradeNo { get; set; }

        /// <summary>
        /// 撤销单号
        /// </summary>
        public string? RevokeTransactionNo { get; set; }

        /// <summary>
        /// 分账金额，单位为分
        /// </summary>
        public long Amount { get; set; }

        /// <summary>
        /// 实际分账金额，单位为分
        /// </summary>
        public long RealAmount { get; set; }

        /// <summary>
        /// 分账结果（00=分账成功，01=分账失败，03=处理中，04=预分帐成功，05=已撤销，51=待处理）
        /// </summary>
        public string SplitState { get; set; }

        /// <summary>
        /// 分账完成时间，格式：YYYYMMDDHHMMSS
        /// </summary>
        public string SplitTime { get; set; }

        /// <summary>
        /// 手续费，单位分
        /// </summary>
        public long ProcedureFee { get; set; }

        /// <summary>
        /// 商户备注
        /// </summary>
        public string? AttachData { get; set; }

        /// <summary>
        /// 分账信息列表
        /// </summary>
        public List<SplitResultInfo> SplitResultInfoList { get; set; }

        /// <summary>
        /// 随机字符串
        /// </summary>
        public string NonceStr { get; set; }
    }

    /// <summary>
    /// 分账结果信息
    /// </summary>
    public class SplitResultInfo
    {
        /// <summary>
        /// 分账目标商户编号
        /// </summary>
        public string CustomerCode { get; set; }

        /// <summary>
        /// 分账金额，单位分
        /// </summary>
        public long Amount { get; set; }

        /// <summary>
        /// 分账手续费，单位分
        /// </summary>
        public long ProcedureFee { get; set; }

        /// <summary>
        /// 结算周期
        /// </summary>
        public int? SettleCycle { get; set; }
    }

    /// <summary>
    /// 分账结果查询请求对象
    /// </summary>
    public class SplitResultQueryRequestDto
    {
        /// <summary>
        /// 商户编号
        /// </summary>
        public string? CustomerCode { get; set; }

        /// <summary>
        /// 版本号
        /// </summary>
        public string? Version { get; set; } = "2.0";

        /// <summary>
        /// 商户订单号
        /// </summary>
        public string? OutTradeNo { get; set; }

        /// <summary>
        /// 易票联订单号
        /// </summary>
        public string? TransactionNo { get; set; }

        /// <summary>
        /// 随机字符串
        /// </summary>
        public string? NonceStr { get; set; }
    }
}
